package com.hangoverstudios.batterysaver;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import com.hangoverstudios.batterysaver.R;

public class BatteryLevelChecker extends Activity{
	
	int battery_level = 0;
	Helper helper;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		battery_level = intent.getIntExtra("battery_level", 0);
		helper = new Helper(this);
		
		displayPopup();
	}
	
	public void displayPopup()
	{
		 AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
	      
	        // Setting Dialog Title
	        alertDialog.setTitle("Battery Low");	  	        
	        alertDialog.setMessage("Battery Low Level "+battery_level+"%. Do you want to enable Power Saver Mode?");	  
	        
	        alertDialog.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog,int which) {
	            	enablePowerSaveMode();	
	            	dialog.dismiss();
	            	//finish();
	            }
	        });
	  
	        // on pressing cancel button
	        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int which) {
	            dialog.cancel();
	            finish();
	            }
	        });
	  
	        // Showing Alert Message
	        alertDialog.setCancelable(false);
	        alertDialog.show();
	        
		/*final Dialog dialog = new Dialog(this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.battery_saver_popup);
		
		TextView text = (TextView)dialog.findViewById(R.id.txt_battery_low);
		
		text.setText("Battery Low Level "+battery_level+"%. Do you want to enable Power Saver Mode?");
		
		Button cancel = (Button)dialog.findViewById(R.id.button_cancel);
		cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				finish();
			}
		});
		Button enable = (Button)dialog.findViewById(R.id.button_enable);
		enable.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				enablePowerSaveMode();	
				dialog.dismiss();
			}
		});

		dialog.show();
		dialog.setCanceledOnTouchOutside(false);*/
	}
	public void enablePowerSaveMode()
	{
		helper.savePowerSaveModeEnabled(true);
		if(helper.isWifiSaverEnabled())
		{
			helper.saveWifiState(Utils.isWifiEnabled(getApplicationContext()));					
			Utils.setWIFIEnabled(getApplicationContext(), false);
			
		}
		if(helper.isBTSaverEnabled())
		{
			helper.saveBTState(Utils.isBluetoothEnabled());
			Utils.setBluetoothEnabled(false);
			
		}
		if(helper.isBrightnessSaverEnabled())
		{	
			int progress = 20;
			float brightness = progress / (float)255;
			WindowManager.LayoutParams lp = getWindow().getAttributes();
			        lp.screenBrightness = brightness;
			        getWindow().setAttributes(lp);
			android.provider.Settings.System.putInt(getContentResolver(),
					android.provider.Settings.System.SCREEN_BRIGHTNESS,
					progress);
		}

		if(helper.isVibrationSaverEnabled())
		{
			helper.saveVibrationState(Utils.isDeviceInVibrateMode(getApplicationContext()));
			Utils.setDeviceMode(getApplicationContext(), AudioManager.RINGER_MODE_VIBRATE);
		}
		if(helper.isMobileDataSaverEnabled())
		{
			helper.saveMobileDataState(Utils.isMobileDataEnabled(getApplicationContext()));
			Utils.setMobileDataEnabled(getApplicationContext(), false);
		}
		finish();

	}

}
